#include "dev_lcd.h"

/* MCU屏相关 */
static SRAM_HandleTypeDef   SRAM_Handler;      /* SRAM句柄(用于控制LCD) */
static dev_mculcd           s_dev_mculcd = {0};  /* MCU屏管理结构体 */

/* RGB屏相关 */
static LTDC_HandleTypeDef   LTDC_Handler;       /* LTDC句柄 */
static DMA2D_HandleTypeDef  DMA2D_Handler;      /* DMA2D句柄 */
static dev_rgblcd           s_dev_rgblcd = {0};  /* RGB屏管理结构体 */
uint16_t                    rgblcd_framebuf[1024][600] __attribute__((at(DT_SDRAM_FOR_RGBLCD_FRAME_BUF_ADDR)));    //定义最大屏分辨率时, LCD所需的帧缓存数组大小
uint32_t                    *ltdc_rgblcd_framebuf[2];   /* LTDC LCD 帧缓存数组指针 */

/* 屏幕管理结构体   */
static dev_lcd              s_dev_lcd = 
{
    .mculcd = &s_dev_mculcd,
    .rgblcd = &s_dev_rgblcd
};

/********************************************************************************************************
 *                                               RGBLCD 驱动
 *******************************************************************************************************/
/* DMA2D初始化 */
//static void s_dev_rgblcd_dma2d_init(void)
//{
//    DMA2D_Handler.Instance                   = DMA2D;
//    DMA2D_Handler.Init.Mode                  = DMA2D_M2M;
//    DMA2D_Handler.Init.ColorMode             = DMA2D_OUTPUT_RGB565;
//    DMA2D_Handler.Init.OutputOffset          = 0;
//    DMA2D_Handler.LayerCfg[1].InputOffset    = 0;
//    DMA2D_Handler.LayerCfg[1].InputColorMode = DEV_RGBLCD_PIXEL_FORMAT_CFG;
//    DMA2D_Handler.LayerCfg[1].AlphaMode      = DMA2D_NO_MODIF_ALPHA;
//
//    DMA2D_Handler.LayerCfg[1].InputAlpha     = 0;
//    if (HAL_DMA2D_Init(&DMA2D_Handler) != HAL_OK)
//    {
//        Error_Handler();
//    }
//    if (HAL_DMA2D_ConfigLayer(&DMA2D_Handler, 1) != HAL_OK)
//    {
//        Error_Handler();
//    }
//
//    return;
//}

/* 打开LCD开关 */
static void s_dev_rgblcd_display_switch(uint8_t status)
{
    if (status == DEV_LCD_DISPLAY_ON)
    {
        __HAL_LTDC_ENABLE(&LTDC_Handler);
    }
    else if (status == DEV_LCD_DISPLAY_OFF)
    {
        __HAL_LTDC_DISABLE(&LTDC_Handler);
    }

    return;
}

/* LTDC时钟(FdClk)设置函数 */
static RET_STATUS s_dev_rgblcd_clk_init(uint32_t pll_sain, uint32_t pll_sair, uint32_t pll_saidivr)
{
    RCC_PeriphCLKInitTypeDef PeriphClkIniture;

    /* LTDC 输出像素时钟 (根据LCD数据手册配置) */
    PeriphClkIniture.PeriphClockSelection = RCC_PERIPHCLK_LTDC; /* LTDC时钟 */
    PeriphClkIniture.PLLSAI.PLLSAIN       = pll_sain;
    PeriphClkIniture.PLLSAI.PLLSAIR       = pll_sair;
    PeriphClkIniture.PLLSAIDivR           = pll_saidivr;

    /* 配置像素时钟 */
    if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkIniture) != HAL_OK)
    {
        return RET_ERROR;
    }

    return RET_OK;
}

/* LTDC 基本参数设置 */
static RET_STATUS s_dev_rgblcd_parameter_config(uint8_t layer, uint32_t bufaddr, uint8_t pixformat, uint8_t alpha, uint8_t alpha0, uint8_t bfac1, uint8_t bfac2, uint32_t bkcolor)
{
    LTDC_LayerCfgTypeDef LTDC_LayerCfg;

    /* 配置 */
    LTDC_LayerCfg.WindowX0        = 0; /* 窗口起始x坐标 */
    LTDC_LayerCfg.WindowY0        = 0; /* 窗口起始y坐标 */
    LTDC_LayerCfg.WindowX1        = s_dev_rgblcd.pwidth;  /* 窗口终止x坐标 */
    LTDC_LayerCfg.WindowY1        = s_dev_rgblcd.pheight;  /* 窗口终止y坐标 */
    LTDC_LayerCfg.PixelFormat     = pixformat;          /* 像素格式 */
    LTDC_LayerCfg.Alpha           = alpha;              /* Alpha值设置(0~255) */
    LTDC_LayerCfg.Alpha0          = alpha0;             /* Alpha值设置(0~255) */
    LTDC_LayerCfg.BlendingFactor1 = (uint32_t) bfac1 << 8; /* 设置层混合系数 */
    LTDC_LayerCfg.BlendingFactor2 = (uint32_t) bfac2 << 8; /* 设置层混合系数 */
    LTDC_LayerCfg.FBStartAdress   = bufaddr;               /* 设置层颜色帧缓存起始地址 */
    LTDC_LayerCfg.ImageWidth      = s_dev_rgblcd.pwidth;    /* 设置颜色帧缓冲区的宽度 */
    LTDC_LayerCfg.ImageHeight     = s_dev_rgblcd.pheight;   /* 设置颜色帧缓冲区的高度 */
    LTDC_LayerCfg.Backcolor.Red   = (uint8_t) (bkcolor & 0x00FF0000) >> 16; /* 背景颜色红色部分 */
    LTDC_LayerCfg.Backcolor.Green = (uint8_t) (bkcolor & 0x0000FF00) >> 8; /* 背景颜色绿色部分 */
    LTDC_LayerCfg.Backcolor.Blue  = (uint8_t) (bkcolor & 0x000000FF); /* 背景颜色蓝色部分 */

    if (HAL_LTDC_ConfigLayer(&LTDC_Handler, &LTDC_LayerCfg, layer) != HAL_OK)
    {
        return RET_ERROR;
    }
    
    return RET_OK;
}

/* LTDC层颜窗口设置 */
static void s_dev_rgblcd_layer_window_config(uint8_t layer, uint16_t sx, uint16_t sy, uint16_t width, uint16_t height)
{
    HAL_LTDC_SetWindowPosition(&LTDC_Handler, sx, sy, layer);
    HAL_LTDC_SetWindowSize(&LTDC_Handler, width, height, layer);

    return;
}

/* 选择层 (0: 第一层, 1: 第二层) */
static void s_dev_rgblcd_set_layer(uint8_t idx)
{
    s_dev_rgblcd.active_layer = idx;

    return;
}

/* 设置LCD显示方向 (0: 竖屏, 1:横屏) */
static void s_dev_rgblcd_set_display_dir(uint8_t ctrl_dir)
{
    if (ctrl_dir == DEV_LCD_CTRL_VER)         /* 竖屏 */
    {
        s_dev_rgblcd.width  = s_dev_rgblcd.pheight;
        s_dev_rgblcd.height = s_dev_rgblcd.pwidth;
    }
    else if (ctrl_dir == DEV_LCD_CTRL_HOR)    /* 横屏 */
    {
        s_dev_rgblcd.width  = s_dev_rgblcd.pwidth;
        s_dev_rgblcd.height = s_dev_rgblcd.pheight;
    }

    /*记录显示方向 */
    s_dev_rgblcd.ctrl_dir = ctrl_dir;

    return;
}

/* RGBLCD 背光控制 */
static void s_dev_rgblcd_backlight_ctrl(uint8_t status)
{
    if (status == DEV_LCD_BACKLIGHT_ON)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
    }
    else if (status == DEV_LCD_BACKLIGHT_OFF)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
    }

    return;
}

/* 画点函数 (x, y 写入坐标, color 颜色值) */
static void s_dev_rgblcd_draw_point(uint16_t x, uint16_t y, uint32_t color)
{
    /* 找到x, y点在显存(数组)中的位置 */
    if (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_HOR)    //横屏
    {
        /*
         *   ------> x(width)
         *   |
         *   |
         *   |y
         */
        *(uint16_t*) ((uint32_t) ltdc_rgblcd_framebuf[s_dev_rgblcd.active_layer] + \
        s_dev_rgblcd.pixsize * (s_dev_rgblcd.pwidth * y + x)) = color;
    }
    else if (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_VER)             //竖屏
    {
        /*
         *   ------> x(扫描方向: 从上到下, 从左到右)
         *   |
         *   |
         *   |y
         */
        *(uint16_t*) ((uint32_t) ltdc_rgblcd_framebuf[s_dev_rgblcd.active_layer] + \
        s_dev_rgblcd.pixsize * (s_dev_rgblcd.pwidth * (y + (s_dev_rgblcd.pheight - x - 1)))) = color; 
    }

    return;
}

/* 屏幕填充 指定区域和指定颜色 函数 */
static void s_dev_rgblcd_fill_color(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint32_t color)
{
    /* 以LCD面板为基准的坐标系,不随横竖屏变化而变化 */
    uint16_t psx, psy, pex, pey;
    uint32_t timeout = 0;
    uint16_t offline;
    uint32_t points_addr;

    if (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_HOR)    /* 横屏 */
    {
        psx = sx;
        psy = sy;
        pex = ex;
        pey = ey;
    }
    else if (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_VER) /* 竖屏 */
    {
        psx = sy;
        psy = s_dev_rgblcd.pheight - ex - 1;
        pex = ey;
        pey = s_dev_rgblcd.pheight - sx - 1;
    }

    offline     = s_dev_rgblcd.pwidth - (pex - psx + 1);    /* 计算行偏移 */
    points_addr = ((uint32_t) ltdc_rgblcd_framebuf[s_dev_rgblcd.active_layer] + \
                   s_dev_rgblcd.pixsize * (s_dev_rgblcd.pwidth * psy + psx));   /* 绘点起始缓冲区地址 */

    /* 配置 DMA2D并启动传输 */
    __HAL_RCC_DMA2D_CLK_ENABLE();    //使能DM2D时钟
    DMA2D->CR      &= ~(DMA2D_CR_START);    //先停止DMA2D
    DMA2D->CR      =  DMA2D_R2M;            //存储器到存储器模式
    DMA2D->OPFCCR  =  DEV_RGBLCD_PIXEL_FORMAT_CFG;    //设置颜色格式
    DMA2D->OOR     =  offline;                //设置行偏移 
    DMA2D->OMAR    =  points_addr;                //输出存储器地址
    DMA2D->NLR     =  ((pex - psx + 1) << 16) | (pey - psy + 1);    //设定行数寄存器 
    DMA2D->OCOLR   =  (uint32_t) color;        //源地址
    DMA2D->CR      |= DMA2D_CR_START;                    //启动DMA2D
    
    /* 等待传输完成 */
    while ((DMA2D->ISR & (DMA2D_FLAG_TC)) == 0)
    {
        timeout++;

        if (timeout > 0x1FFFFF)
        {
            break;    //超时退出
        }
    } 

    DMA2D->IFCR |= DMA2D_FLAG_TC;                //清除传输完成标志

    return;
}

/* 屏幕填充 指定区域和颜色集 函数 */
static void s_dev_rgblcd_fill_colors(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t *colors)
{
    /* 以LCD面板为基准的坐标系,不随横竖屏变化而变化 */
    uint16_t psx, psy, pex, pey;
    uint32_t timeout = 0;
    uint16_t offline;
    uint32_t points_addr;

    if (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_HOR)
    {
        psx = sx;
        psy = sy;
        pex = ex;
        pey = ey;
    }
    else if (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_VER)
    {
        psx = sy;
        psy = s_dev_rgblcd.pheight - ex - 1;
        pex = ey;
        pey = s_dev_rgblcd.pheight - sx - 1;
    }

    offline     = s_dev_rgblcd.pwidth - (pex - psx + 1);    /* 计算行偏移 */
    points_addr = ((uint32_t) ltdc_rgblcd_framebuf[s_dev_rgblcd.active_layer] + \
                   s_dev_rgblcd.pixsize * (s_dev_rgblcd.pwidth * psy + psx));   /* 绘点起始缓冲区地址 */

    /* 配置 DMA2D并启动传输 */
    __HAL_RCC_DMA2D_CLK_ENABLE();    //使能DM2D时钟
    //DMA2D->CR      &= ~(DMA2D_CR_START);    //先停止DMA2D
    DMA2D->CR      =  DMA2D_M2M;            //存储器到存储器模式
    DMA2D->FGPFCCR =  DEV_RGBLCD_PIXEL_FORMAT_CFG;    //设置颜色格式
    DMA2D->FGOR    =  0;                    //前景层行偏移为0
    DMA2D->OOR     =  offline;                //设置行偏移 

    DMA2D->FGMAR   =  (uint32_t) colors;        //源地址
    DMA2D->OMAR    =  points_addr;                //输出存储器地址
    DMA2D->NLR     =  ((pex - psx + 1) << 16) | (pey - psy + 1);    //设定行数寄存器 
    DMA2D->CR      |= DMA2D_CR_START;   /* 启动DMA2D */
    
    /* 等待传输完成 */
    while ((DMA2D->ISR & (DMA2D_FLAG_TC)) == 0)
    {
        timeout++;

        if (timeout > 0x1FFFFF)
        {
            break;    //超时退出
        }
    } 

    DMA2D->IFCR |= DMA2D_FLAG_TC;                //清除传输完成标志

    return;
}

/* 清屏函数 */
static void s_dev_rgblcd_clear(void)
{
    /* 填入白色 */
    s_dev_rgblcd_fill_color(0, 0, (s_dev_rgblcd.width - 1), (s_dev_rgblcd.height - 1), LCD_COLOR_WHITE);

    return;
}

/* 读取屏幕的型号区分管脚(适配正点原子的屏幕) */
static uint16_t s_dev_rgblcd_get_id(void)
{
    uint8_t          mx;
    uint16_t         id;
    GPIO_InitTypeDef GPIO_Initure;

    __HAL_RCC_GPIOG_CLK_ENABLE();
    __HAL_RCC_GPIOI_CLK_ENABLE();

    GPIO_Initure.Pin=GPIO_PIN_6;        //PG6
    GPIO_Initure.Mode=GPIO_MODE_INPUT;  //输入
    GPIO_Initure.Pull=GPIO_PULLUP;      //上拉
    GPIO_Initure.Speed=GPIO_SPEED_HIGH; //高速
    HAL_GPIO_Init(GPIOG, &GPIO_Initure); //初始化
    
    GPIO_Initure.Pin=GPIO_PIN_2|GPIO_PIN_7; //PI2,7
    HAL_GPIO_Init(GPIOI, &GPIO_Initure);     //初始化

    /* 读取管脚电平 */
    mx =  (uint8_t) HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_6); //读取M0
    mx |= (uint8_t) HAL_GPIO_ReadPin(GPIOI, GPIO_PIN_2) << 1; //读取M1
    mx |= (uint8_t) HAL_GPIO_ReadPin(GPIOI, GPIO_PIN_7) << 2; //读取M2

    /* 根据读取到的管脚电平组合, 识别LCD屏 */
    switch (mx)
    {
        case 0: id = DEV_RGBLCD_4_3_R480X272_ID;   /* 4.3寸屏, 480*272分辨率 */
            break;

        case 1: id = DEV_RGBLCD_7_R800X480_ID;     /* 7寸屏, 800*480分辨率 */
            break;

        case 2: id = DEV_RGBLCD_7_R1024X600_ID;    /* 7寸屏, 1024*600分辨率 */
            break;

        default: id = DT_INVAL_MARK;               /* 未识别 */
            break;
    }

    /* 记录LCD屏幕的ID */
    s_dev_rgblcd.id = id;

    return id;
}

/* RGBLCD 初始化函数 */
static RET_STATUS s_dev_rgblcd_init(void)
{
    uint16_t id;

    /* 获取屏幕ID */
    id = s_dev_rgblcd_get_id();

    /* 根据不同屏幕型号, 设置参数 */
    if (id == DEV_RGBLCD_4_3_R480X272_ID)
    {
        s_dev_rgblcd.pwidth  = 480;            //面板宽度,单位:像素
        s_dev_rgblcd.pheight = 272;            //面板高度,单位:像素
        s_dev_rgblcd.hsw     = 1;              //水平同步宽度
        s_dev_rgblcd.vsw     = 1;              //垂直同步宽度
        s_dev_rgblcd.hbp     = 40;             //水平后廊
        s_dev_rgblcd.vbp     = 8;              //垂直后廊
        s_dev_rgblcd.hfp     = 5;              //水平前廊
        s_dev_rgblcd.vfp     = 8;              //垂直前廊

        /* LTDC时钟(FdClk)设置函数 */
        s_dev_rgblcd_clk_init(288, 4, RCC_PLLSAIDIVR_4);
    }
    else if (id == DEV_RGBLCD_7_R800X480_ID)
    {
        s_dev_rgblcd.pwidth  = 800;            //面板宽度,单位:像素
        s_dev_rgblcd.pheight = 480;            //面板高度,单位:像素
        s_dev_rgblcd.hsw     = 1;              //水平同步宽度
        s_dev_rgblcd.vsw     = 1;              //垂直同步宽度
        s_dev_rgblcd.hbp     = 46;             //水平后廊
        s_dev_rgblcd.vbp     = 23;              //垂直后廊
        s_dev_rgblcd.hfp     = 210;              //水平前廊
        s_dev_rgblcd.vfp     = 22;              //垂直前廊

        /* LTDC时钟(FdClk)设置函数 */
        s_dev_rgblcd_clk_init(396, 3, RCC_PLLSAIDIVR_4);
    }
    else if (id == DEV_RGBLCD_7_R1024X600_ID)
    {
        s_dev_rgblcd.pwidth  = 1024;            //面板宽度,单位:像素
        s_dev_rgblcd.pheight = 600;            //面板高度,单位:像素
        s_dev_rgblcd.hsw     = 20;              //水平同步宽度
        s_dev_rgblcd.vsw     = 3;              //垂直同步宽度
        s_dev_rgblcd.hbp     = 140;             //水平后廊
        s_dev_rgblcd.vbp     = 20;              //垂直后廊
        s_dev_rgblcd.hfp     = 160;              //水平前廊
        s_dev_rgblcd.vfp     = 12;              //垂直前廊

        /* LTDC时钟(FdClk)设置函数 */
        s_dev_rgblcd_clk_init(360, 2, RCC_PLLSAIDIVR_4);    //设置像素时钟  45Mhz 
    }
    else
    {
        return RET_ERROR; /* 未知型号 */
    }

    /* 记录屏幕尺寸 */
    s_dev_rgblcd.width   = s_dev_rgblcd.pwidth;
    s_dev_rgblcd.height  = s_dev_rgblcd.pheight;
    s_dev_rgblcd.pixsize = 2;  /* 每个像素所占字节数 */
        
    ltdc_rgblcd_framebuf[0] = (uint32_t *) &rgblcd_framebuf;  /* 指定缓冲区地址 */

    /* LTDC配置 */
    LTDC_Handler.Instance                = LTDC;
    LTDC_Handler.Init.HSPolarity         = LTDC_HSPOLARITY_AL;         //水平同步极性
    LTDC_Handler.Init.VSPolarity         = LTDC_VSPOLARITY_AL;         //垂直同步极性
    LTDC_Handler.Init.DEPolarity         = LTDC_DEPOLARITY_AL;         //数据使能极性

    if (id == DEV_RGBLCD_10_1_R1280X800_ID)
    {
        LTDC_Handler.Init.PCPolarity     = LTDC_PCPOLARITY_IIPC;       //像素时钟极性
    }
    else 
    {
        LTDC_Handler.Init.PCPolarity     = LTDC_PCPOLARITY_IPC;       //像素时钟极性
    }

    LTDC_Handler.Init.HorizontalSync     = s_dev_rgblcd.hsw - 1;          //水平同步宽度
    LTDC_Handler.Init.VerticalSync       = s_dev_rgblcd.vsw - 1;          //垂直同步宽度
    LTDC_Handler.Init.AccumulatedHBP     = s_dev_rgblcd.hsw + s_dev_rgblcd.hbp - 1; //水平同步后沿宽度
    LTDC_Handler.Init.AccumulatedVBP     = s_dev_rgblcd.vsw + s_dev_rgblcd.vbp - 1; //垂直同步后沿高度
    LTDC_Handler.Init.AccumulatedActiveW = s_dev_rgblcd.hsw + s_dev_rgblcd.hbp + s_dev_rgblcd.pwidth - 1;//有效宽度
    LTDC_Handler.Init.AccumulatedActiveH = s_dev_rgblcd.vsw + s_dev_rgblcd.vbp + s_dev_rgblcd.pheight - 1;//有效高度
    LTDC_Handler.Init.TotalWidth         = s_dev_rgblcd.hsw + s_dev_rgblcd.hbp + s_dev_rgblcd.pwidth + s_dev_rgblcd.hfp - 1;   //总宽度
    LTDC_Handler.Init.TotalHeigh         = s_dev_rgblcd.vsw + s_dev_rgblcd.vbp + s_dev_rgblcd.pheight + s_dev_rgblcd.vfp - 1;  //总高度
    LTDC_Handler.Init.Backcolor.Red      = 0;           //屏幕背景层红色部分
    LTDC_Handler.Init.Backcolor.Green    = 0;           //屏幕背景层绿色部分
    LTDC_Handler.Init.Backcolor.Blue     = 0;           //屏幕背景色蓝色部分

    if (HAL_LTDC_Init(&LTDC_Handler) != HAL_OK)
    {
        return RET_ERROR;
    }

    /* 设置屏幕方向 */
    s_dev_rgblcd_set_display_dir(DEV_LCD_CTRL_DIR_DEFAULT);

    /* 层参数配置 */
    if (s_dev_rgblcd_parameter_config(0, (uint32_t) ltdc_rgblcd_framebuf[0], DEV_RGBLCD_PIXEL_FORMAT_CFG, \
                                      255, 0, 6, 7, LCD_COLOR_BLACK) != RET_OK)
    {
        return RET_ERROR;
    }

    /* LTDC层颜窗口设置 */
    s_dev_rgblcd_layer_window_config(0, 0, 0, s_dev_rgblcd.pwidth, s_dev_rgblcd.pheight);

    /* 选择显示层(第0层) */
    s_dev_rgblcd_set_layer(0);

    /* 点亮背光 */
    s_dev_rgblcd_backlight_ctrl(DEV_LCD_BACKLIGHT_ON);

    /* 清屏 */
    s_dev_rgblcd_clear();

    return RET_OK;
}

/* 显示 屏幕信息 */
static void s_dev_rgblcd_show_info(void)
{
    printf("RGBLCD info: \r\n");
    printf("ID: 0x%x\r\n", s_dev_rgblcd.id);    /* id */
    printf("Width = %d, Height: %d\r\n", s_dev_rgblcd.width, s_dev_rgblcd.height); /* 显示器宽和高 */
    printf("Ctrl Dir = %s\r\n", (s_dev_rgblcd.ctrl_dir == DEV_LCD_CTRL_VER) ? "vertical" : "horizontal"); /* 显示方向 */

    return;
}

/* LTDC底层IO初始化和时钟使能 */
void HAL_LTDC_MspInit(LTDC_HandleTypeDef* hltdc)
{
    GPIO_InitTypeDef GPIO_Initure;
    
    __HAL_RCC_LTDC_CLK_ENABLE();
    __HAL_RCC_DMA2D_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOF_CLK_ENABLE();
    __HAL_RCC_GPIOG_CLK_ENABLE();
    __HAL_RCC_GPIOH_CLK_ENABLE();
    __HAL_RCC_GPIOI_CLK_ENABLE();
    
    //背光引脚
    GPIO_Initure.Pin=GPIO_PIN_5;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;
    GPIO_Initure.Pull=GPIO_PULLUP;
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);
    
    //初始化管脚
    GPIO_Initure.Pin=GPIO_PIN_10; 
    GPIO_Initure.Mode=GPIO_MODE_AF_PP;
    GPIO_Initure.Pull=GPIO_NOPULL;
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;
    GPIO_Initure.Alternate=GPIO_AF14_LTDC;  /* 复用为LTDC */
    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
    
    GPIO_Initure.Pin=GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_11;
    HAL_GPIO_Init(GPIOG,&GPIO_Initure);
    
    GPIO_Initure.Pin=GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|\
                     GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
    HAL_GPIO_Init(GPIOH,&GPIO_Initure);
    
    GPIO_Initure.Pin=GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5|\
                     GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_9|GPIO_PIN_10;
    HAL_GPIO_Init(GPIOI,&GPIO_Initure); 

    return;
}

/********************************************************************************************************
 *                                               MCULCD 屏驱动
 *******************************************************************************************************/

//写寄存器函数
//regval:寄存器值
static void S_DEV_MCULCD_WR_REG(uint16_t value)
{
    value    = value;   /* 使用-O2优化的时候,必须插入的延时 */
    LCD->Reg = value;   /* 写入要写的寄存器序号 */

    return;
}

//写LCD数据
//data:要写入的值
static void S_DEV_MCULCD_WR_DATA(uint16_t data)
{
    data     = data;   /* 使用-O2优化的时候,必须插入的延时 */
    LCD->Ram = data;   /* 写入要写的寄存器序号 */

    return;
}

//读LCD数据
//返回值:读到的值
static uint16_t S_DEV_MCULCD_RD_DATA(void)
{
    volatile uint16_t data;
    data = LCD->Ram;

    return data;
}

//写寄存器
//reg: 寄存器地址
//value: 要写入的数据
static void S_DEV_MCULCD_WRITE_REG(uint16_t reg, uint16_t value)
{
    LCD->Reg = reg;     //写入要写的寄存器序号
    LCD->Ram = value;   //写入数据

    return;
}

//读寄存器
//reg:寄存器地址
//返回值:读到的数据
static uint16_t S_DEV_MCULCD_READ_REG(uint16_t reg)
{
    uint16_t data;

    S_DEV_MCULCD_WR_REG(reg);             //写入要读的寄存器序号
    soc_time_delay_us(5);
    data = S_DEV_MCULCD_RD_DATA();        //返回读到的值

    return data;
}

//开始写GRAM
static void s_dev_mculcd_write_gram_cmd(void)
{
    LCD->Reg = s_dev_mculcd.wr_gram_cmd;

    return;
}

//LCD写GRAM
//RGB_Code:颜色值
static void s_dev_mculcd_write_gram(uint16_t rgb_code)
{
    LCD->Ram = rgb_code;  /* 写16位GRAM */

    return;
}

//SSD1963 背光设置
//pwm:背光等级,0~100.越大越亮.
static void s_dev_mculcd_set_ssd1963_backlight(uint8_t pwm)
{
    S_DEV_MCULCD_WR_REG(0xBE);    //配置PWM输出
    S_DEV_MCULCD_WR_DATA(0x05);    //1设置PWM频率
    S_DEV_MCULCD_WR_DATA(pwm * 2.55);//2设置PWM占空比
    S_DEV_MCULCD_WR_DATA(0x01);    //3设置C
    S_DEV_MCULCD_WR_DATA(0xFF);    //4设置D
    S_DEV_MCULCD_WR_DATA(0x00);    //5设置E
    S_DEV_MCULCD_WR_DATA(0x00);    //6设置F

    return;
}

/* 获得LCD设备ID */
static uint16_t s_dev_mculcd_get_id(void)
{
    uint16_t dev_id = 0;

    /* 尝试 0x9341 */
    S_DEV_MCULCD_WR_REG(0xD3);
    dev_id =  S_DEV_MCULCD_RD_DATA();            //dummy read
    dev_id =  S_DEV_MCULCD_RD_DATA();            //读到0X00
    dev_id =  S_DEV_MCULCD_RD_DATA();            //读到0X93
    dev_id <<= 8;
    dev_id |= S_DEV_MCULCD_RD_DATA();            //读到0X41

    if (dev_id != DEV_LCD_ILI9341_ID)
    {
        /* 尝试 0x5310 */
        S_DEV_MCULCD_WR_REG(0XD4);
        dev_id =  S_DEV_MCULCD_RD_DATA();            //dummy read
        dev_id =  S_DEV_MCULCD_RD_DATA();            //读到0X01
        dev_id =  S_DEV_MCULCD_RD_DATA();            //读到0X53
        dev_id <<= 8;
        dev_id |= S_DEV_MCULCD_RD_DATA();            //读到0X10

        if (dev_id != DEV_LCD_NT35310_ID)
        {
            /* 尝试0x5510 */
            S_DEV_MCULCD_WR_REG(0XDA00);
            dev_id =  S_DEV_MCULCD_RD_DATA();            //读回0X00
            S_DEV_MCULCD_WR_REG(0XDB00);
            dev_id =  S_DEV_MCULCD_RD_DATA();            //读回0X80
            dev_id <<= 8;
            S_DEV_MCULCD_WR_REG(0XDB00);
            dev_id |= S_DEV_MCULCD_RD_DATA();            //读到0X00

            if (dev_id != DEV_LCD_NT35510_ID)
            {
                /* 尝试0x1963 */
                S_DEV_MCULCD_WR_REG(0XA1);
                dev_id =  S_DEV_MCULCD_RD_DATA();            //dummy read
                dev_id =  S_DEV_MCULCD_RD_DATA();            //读到0X57
                dev_id <<= 8;
                dev_id |= S_DEV_MCULCD_RD_DATA();            //读到0X61

                if (dev_id != DEV_LCD_SSD1963_ID)
                {
                    dev_id = DT_INVAL_MARK;                  /* 未匹配到型号, 标记数据无效 */
                }
            }
        }
    }

    /* 记录ID */
    s_dev_mculcd.id = dev_id;

    return s_dev_mculcd.id;
}

/* LCD寄存器配置 */
static RET_STATUS s_dev_mculcd_reg_init(uint16_t dev_id)
{
    RET_STATUS ret_status = RET_OK;

    switch (dev_id)
    {
        case DEV_LCD_ILI9341_ID:
            S_DEV_MCULCD_WR_REG(0xCF);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC1);
            S_DEV_MCULCD_WR_DATA(0X30);
            S_DEV_MCULCD_WR_REG(0xED);
            S_DEV_MCULCD_WR_DATA(0x64);
            S_DEV_MCULCD_WR_DATA(0x03);
            S_DEV_MCULCD_WR_DATA(0X12);
            S_DEV_MCULCD_WR_DATA(0X81);
            S_DEV_MCULCD_WR_REG(0xE8);
            S_DEV_MCULCD_WR_DATA(0x85);
            S_DEV_MCULCD_WR_DATA(0x10);
            S_DEV_MCULCD_WR_DATA(0x7A);
            S_DEV_MCULCD_WR_REG(0xCB);
            S_DEV_MCULCD_WR_DATA(0x39);
            S_DEV_MCULCD_WR_DATA(0x2C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x34);
            S_DEV_MCULCD_WR_DATA(0x02);
            S_DEV_MCULCD_WR_REG(0xF7);
            S_DEV_MCULCD_WR_DATA(0x20);
            S_DEV_MCULCD_WR_REG(0xEA);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_REG(0xC0);    //Power control 
            S_DEV_MCULCD_WR_DATA(0x1B);   //VRH[5:0] 
            S_DEV_MCULCD_WR_REG(0xC1);    //Power control 
            S_DEV_MCULCD_WR_DATA(0x01);   //SAP[2:0];BT[3:0] 
            S_DEV_MCULCD_WR_REG(0xC5);    //VCM control 
            S_DEV_MCULCD_WR_DATA(0x30);      //3F
            S_DEV_MCULCD_WR_DATA(0x30);      //3C
            S_DEV_MCULCD_WR_REG(0xC7);    //VCM control2 
            S_DEV_MCULCD_WR_DATA(0XB7);
            S_DEV_MCULCD_WR_REG(0x36);    // Memory Access Control 
            S_DEV_MCULCD_WR_DATA(0x48);
            S_DEV_MCULCD_WR_REG(0x3A);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_REG(0xB1);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x1A);
            S_DEV_MCULCD_WR_REG(0xB6);    // Display Function Control 
            S_DEV_MCULCD_WR_DATA(0x0A);
            S_DEV_MCULCD_WR_DATA(0xA2);
            S_DEV_MCULCD_WR_REG(0xF2);    // 3Gamma Function Disable 
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_REG(0x26);    //Gamma curve selected 
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_REG(0xE0);    //Set Gamma 
            S_DEV_MCULCD_WR_DATA(0x0F);
            S_DEV_MCULCD_WR_DATA(0x2A);
            S_DEV_MCULCD_WR_DATA(0x28);
            S_DEV_MCULCD_WR_DATA(0x08);
            S_DEV_MCULCD_WR_DATA(0x0E);
            S_DEV_MCULCD_WR_DATA(0x08);
            S_DEV_MCULCD_WR_DATA(0x54);
            S_DEV_MCULCD_WR_DATA(0XA9);
            S_DEV_MCULCD_WR_DATA(0x43);
            S_DEV_MCULCD_WR_DATA(0x0A);
            S_DEV_MCULCD_WR_DATA(0x0F);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_REG(0XE1);    //Set Gamma 
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x15);
            S_DEV_MCULCD_WR_DATA(0x17);
            S_DEV_MCULCD_WR_DATA(0x07);
            S_DEV_MCULCD_WR_DATA(0x11);
            S_DEV_MCULCD_WR_DATA(0x06);
            S_DEV_MCULCD_WR_DATA(0x2B);
            S_DEV_MCULCD_WR_DATA(0x56);
            S_DEV_MCULCD_WR_DATA(0x3C);
            S_DEV_MCULCD_WR_DATA(0x05);
            S_DEV_MCULCD_WR_DATA(0x10);
            S_DEV_MCULCD_WR_DATA(0x0F);
            S_DEV_MCULCD_WR_DATA(0x3F);
            S_DEV_MCULCD_WR_DATA(0x3F);
            S_DEV_MCULCD_WR_DATA(0x0F);
            S_DEV_MCULCD_WR_REG(0x2B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_DATA(0x3f);
            S_DEV_MCULCD_WR_REG(0x2A);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xef);
            S_DEV_MCULCD_WR_REG(0x11); //Exit Sleep
            soc_time_delay_ms(120);
            S_DEV_MCULCD_WR_REG(0x29); //display on
            break;

        case DEV_LCD_NT35310_ID:
            S_DEV_MCULCD_WR_REG(0xED);
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_DATA(0xFE);
    
            S_DEV_MCULCD_WR_REG(0xEE);
            S_DEV_MCULCD_WR_DATA(0xDE);
            S_DEV_MCULCD_WR_DATA(0x21);
    
            S_DEV_MCULCD_WR_REG(0xF1);
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_REG(0xDF);
            S_DEV_MCULCD_WR_DATA(0x10);
    
            //VCOMvoltage//
            S_DEV_MCULCD_WR_REG(0xC4);
            S_DEV_MCULCD_WR_DATA(0x8F);      //5f
    
            S_DEV_MCULCD_WR_REG(0xC6);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xE2);
            S_DEV_MCULCD_WR_DATA(0xE2);
            S_DEV_MCULCD_WR_DATA(0xE2);
            S_DEV_MCULCD_WR_REG(0xBF);
            S_DEV_MCULCD_WR_DATA(0xAA);
    
            S_DEV_MCULCD_WR_REG(0xB0);
            S_DEV_MCULCD_WR_DATA(0x0D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x0D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x11);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x19);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x21);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x2D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x5D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x5D);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB1);
            S_DEV_MCULCD_WR_DATA(0x80);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x8B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x96);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x02);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x03);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB4);
            S_DEV_MCULCD_WR_DATA(0x8B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x96);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA1);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB5);
            S_DEV_MCULCD_WR_DATA(0x02);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x03);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x04);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB6);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB7);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3F);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x5E);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x64);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x8C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xAC);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xDC);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x70);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x90);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xEB);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xDC);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xB8);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xBA);
            S_DEV_MCULCD_WR_DATA(0x24);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC1);
            S_DEV_MCULCD_WR_DATA(0x20);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x54);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xFF);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC2);
            S_DEV_MCULCD_WR_DATA(0x0A);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x04);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC3);
            S_DEV_MCULCD_WR_DATA(0x3C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3A);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x39);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x37);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x36);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x32);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x2F);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x2C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x29);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x26);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x24);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x24);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x23);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x36);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x32);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x2F);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x2C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x29);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x26);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x24);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x24);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x23);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC4);
            S_DEV_MCULCD_WR_DATA(0x62);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x05);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x84);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF0);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x18);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA4);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x18);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x50);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x0C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x17);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x95);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xE6);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC5);
            S_DEV_MCULCD_WR_DATA(0x32);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x65);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x76);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x88);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC6);
            S_DEV_MCULCD_WR_DATA(0x20);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x17);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC7);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC8);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xC9);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE0);
            S_DEV_MCULCD_WR_DATA(0x16);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x1C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x21);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x36);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x46);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x52);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x64);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x7A);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x8B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA8);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xB9);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC4);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xCA);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD9);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xE0);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE1);
            S_DEV_MCULCD_WR_DATA(0x16);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x1C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x22);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x36);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x45);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x52);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x64);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x7A);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x8B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA8);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xB9);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC4);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xCA);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD8);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xE0);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE2);
            S_DEV_MCULCD_WR_DATA(0x05);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x0B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x1B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x34);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x4F);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x61);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x79);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x88);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x97);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA6);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xB7);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC7);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD1);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD6);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xDD);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_REG(0xE3);
            S_DEV_MCULCD_WR_DATA(0x05);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x1C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x33);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x50);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x62);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x78);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x88);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x97);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA6);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xB7);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC7);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD1);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD5);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xDD);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE4);
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x01);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x02);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x2A);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x4B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x5D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x74);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x84);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x93);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xB3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xBE);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC4);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xCD);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xDD);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_REG(0xE5);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x02);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x29);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x3C);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x4B);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x5D);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x74);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x84);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x93);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xA2);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xB3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xBE);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xC4);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xCD);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xD3);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xDC);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE6);
            S_DEV_MCULCD_WR_DATA(0x11);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x34);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x56);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x76);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x77);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x66);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x88);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xBB);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x66);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x45);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x43);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE7);
            S_DEV_MCULCD_WR_DATA(0x32);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x76);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x66);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x67);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x67);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x87);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xBB);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x77);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x56);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x23); 
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x33);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x45);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE8);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x87);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x88);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x77);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x66);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x88);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xAA);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0xBB);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x99);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x66);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x44);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x55);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xE9);
            S_DEV_MCULCD_WR_DATA(0xAA);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0x00);
            S_DEV_MCULCD_WR_DATA(0xAA);
    
            S_DEV_MCULCD_WR_REG(0xCF);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xF0);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x50);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xF3);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xF9);
            S_DEV_MCULCD_WR_DATA(0x06);
            S_DEV_MCULCD_WR_DATA(0x10);
            S_DEV_MCULCD_WR_DATA(0x29);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0x3A);
            S_DEV_MCULCD_WR_DATA(0x55);    //66
    
            S_DEV_MCULCD_WR_REG(0x11);
            soc_time_delay_ms(100);
            S_DEV_MCULCD_WR_REG(0x29);
            S_DEV_MCULCD_WR_REG(0x35);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0x51);
            S_DEV_MCULCD_WR_DATA(0xFF);
            S_DEV_MCULCD_WR_REG(0x53);
            S_DEV_MCULCD_WR_DATA(0x2C);
            S_DEV_MCULCD_WR_REG(0x55);
            S_DEV_MCULCD_WR_DATA(0x82);
            S_DEV_MCULCD_WR_REG(0x2c);
            break;

        case DEV_LCD_NT35510_ID:
            S_DEV_MCULCD_WRITE_REG(0xF000, 0x55);
            S_DEV_MCULCD_WRITE_REG(0xF001, 0xAA);
            S_DEV_MCULCD_WRITE_REG(0xF002, 0x52);
            S_DEV_MCULCD_WRITE_REG(0xF003, 0x08);
            S_DEV_MCULCD_WRITE_REG(0xF004, 0x01);
            //AVDD Set AVDD 5.2V 
            S_DEV_MCULCD_WRITE_REG(0xB000, 0x0D);
            S_DEV_MCULCD_WRITE_REG(0xB001, 0x0D);
            S_DEV_MCULCD_WRITE_REG(0xB002, 0x0D);
            //AVDD ratio 
            S_DEV_MCULCD_WRITE_REG(0xB600, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xB601, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xB602, 0x34);
            //AVEE -5.2V 
            S_DEV_MCULCD_WRITE_REG(0xB100, 0x0D);
            S_DEV_MCULCD_WRITE_REG(0xB101, 0x0D);
            S_DEV_MCULCD_WRITE_REG(0xB102, 0x0D);
            //AVEE ratio 
            S_DEV_MCULCD_WRITE_REG(0xB700, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xB701, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xB702, 0x34);
            //VCL -2.5V 
            S_DEV_MCULCD_WRITE_REG(0xB200, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xB201, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xB202, 0x00);
            //VCL ratio
            S_DEV_MCULCD_WRITE_REG(0xB800, 0x24);
            S_DEV_MCULCD_WRITE_REG(0xB801, 0x24);
            S_DEV_MCULCD_WRITE_REG(0xB802, 0x24);
            //VGH 15V (Free pump) 
            S_DEV_MCULCD_WRITE_REG(0xBF00, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xB300, 0x0F);
            S_DEV_MCULCD_WRITE_REG(0xB301, 0x0F);
            S_DEV_MCULCD_WRITE_REG(0xB302, 0x0F);
            //VGH ratio 
            S_DEV_MCULCD_WRITE_REG(0xB900, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xB901, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xB902, 0x34);
            //VGL_REG -10V 
            S_DEV_MCULCD_WRITE_REG(0xB500, 0x08);
            S_DEV_MCULCD_WRITE_REG(0xB501, 0x08);
            S_DEV_MCULCD_WRITE_REG(0xB502, 0x08);
            S_DEV_MCULCD_WRITE_REG(0xC200, 0x03);
            //VGLX ratio 
            S_DEV_MCULCD_WRITE_REG(0xBA00, 0x24);
            S_DEV_MCULCD_WRITE_REG(0xBA01, 0x24);
            S_DEV_MCULCD_WRITE_REG(0xBA02, 0x24);
            //VGMP/VGSP 4.5V/0V 
            S_DEV_MCULCD_WRITE_REG(0xBC00, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xBC01, 0x78);
            S_DEV_MCULCD_WRITE_REG(0xBC02, 0x00);
            //VGMN/VGSN -4.5V/0V 
            S_DEV_MCULCD_WRITE_REG(0xBD00, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xBD01, 0x78);
            S_DEV_MCULCD_WRITE_REG(0xBD02, 0x00);
            //VCOM 
            S_DEV_MCULCD_WRITE_REG(0xBE00, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xBE01, 0x64);
            //Gamma Setting 
            S_DEV_MCULCD_WRITE_REG(0xD100, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD101, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD102, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD103, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD104, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD105, 0x3A);
            S_DEV_MCULCD_WRITE_REG(0xD106, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD107, 0x4A);
            S_DEV_MCULCD_WRITE_REG(0xD108, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD109, 0x5C);
            S_DEV_MCULCD_WRITE_REG(0xD10A, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD10B, 0x81);
            S_DEV_MCULCD_WRITE_REG(0xD10C, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD10D, 0xA6);
            S_DEV_MCULCD_WRITE_REG(0xD10E, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD10F, 0xE5);
            S_DEV_MCULCD_WRITE_REG(0xD110, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD111, 0x13);
            S_DEV_MCULCD_WRITE_REG(0xD112, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD113, 0x54);
            S_DEV_MCULCD_WRITE_REG(0xD114, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD115, 0x82);
            S_DEV_MCULCD_WRITE_REG(0xD116, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD117, 0xCA);
            S_DEV_MCULCD_WRITE_REG(0xD118, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD119, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD11A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD11B, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD11C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD11D, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD11E, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD11F, 0x67);
            S_DEV_MCULCD_WRITE_REG(0xD120, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD121, 0x84);
            S_DEV_MCULCD_WRITE_REG(0xD122, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD123, 0xA4);
            S_DEV_MCULCD_WRITE_REG(0xD124, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD125, 0xB7);
            S_DEV_MCULCD_WRITE_REG(0xD126, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD127, 0xCF);
            S_DEV_MCULCD_WRITE_REG(0xD128, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD129, 0xDE);
            S_DEV_MCULCD_WRITE_REG(0xD12A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD12B, 0xF2);
            S_DEV_MCULCD_WRITE_REG(0xD12C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD12D, 0xFE);
            S_DEV_MCULCD_WRITE_REG(0xD12E, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD12F, 0x10);
            S_DEV_MCULCD_WRITE_REG(0xD130, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD131, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD132, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD133, 0x6D);
            S_DEV_MCULCD_WRITE_REG(0xD200, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD201, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD202, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD203, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD204, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD205, 0x3A);
            S_DEV_MCULCD_WRITE_REG(0xD206, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD207, 0x4A);
            S_DEV_MCULCD_WRITE_REG(0xD208, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD209, 0x5C);
            S_DEV_MCULCD_WRITE_REG(0xD20A, 0x00);
 
            S_DEV_MCULCD_WRITE_REG(0xD20B, 0x81);
            S_DEV_MCULCD_WRITE_REG(0xD20C, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD20D, 0xA6);
            S_DEV_MCULCD_WRITE_REG(0xD20E, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD20F, 0xE5);
            S_DEV_MCULCD_WRITE_REG(0xD210, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD211, 0x13);
            S_DEV_MCULCD_WRITE_REG(0xD212, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD213, 0x54);
            S_DEV_MCULCD_WRITE_REG(0xD214, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD215, 0x82);
            S_DEV_MCULCD_WRITE_REG(0xD216, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD217, 0xCA);
            S_DEV_MCULCD_WRITE_REG(0xD218, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD219, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD21A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD21B, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD21C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD21D, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD21E, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD21F, 0x67);
            S_DEV_MCULCD_WRITE_REG(0xD220, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD221, 0x84);
            S_DEV_MCULCD_WRITE_REG(0xD222, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD223, 0xA4);
            S_DEV_MCULCD_WRITE_REG(0xD224, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD225, 0xB7);
            S_DEV_MCULCD_WRITE_REG(0xD226, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD227, 0xCF);
            S_DEV_MCULCD_WRITE_REG(0xD228, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD229, 0xDE);
            S_DEV_MCULCD_WRITE_REG(0xD22A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD22B, 0xF2);
            S_DEV_MCULCD_WRITE_REG(0xD22C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD22D, 0xFE);
            S_DEV_MCULCD_WRITE_REG(0xD22E, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD22F, 0x10);
            S_DEV_MCULCD_WRITE_REG(0xD230, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD231, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD232, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD233, 0x6D);
            S_DEV_MCULCD_WRITE_REG(0xD300, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD301, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD302, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD303, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD304, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD305, 0x3A);
            S_DEV_MCULCD_WRITE_REG(0xD306, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD307, 0x4A);
            S_DEV_MCULCD_WRITE_REG(0xD308, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD309, 0x5C);
            S_DEV_MCULCD_WRITE_REG(0xD30A, 0x00);
 
            S_DEV_MCULCD_WRITE_REG(0xD30B, 0x81);
            S_DEV_MCULCD_WRITE_REG(0xD30C, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD30D, 0xA6);
            S_DEV_MCULCD_WRITE_REG(0xD30E, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD30F, 0xE5);
            S_DEV_MCULCD_WRITE_REG(0xD310, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD311, 0x13);
            S_DEV_MCULCD_WRITE_REG(0xD312, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD313, 0x54);
            S_DEV_MCULCD_WRITE_REG(0xD314, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD315, 0x82);
            S_DEV_MCULCD_WRITE_REG(0xD316, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD317, 0xCA);
            S_DEV_MCULCD_WRITE_REG(0xD318, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD319, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD31A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD31B, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD31C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD31D, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD31E, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD31F, 0x67);
            S_DEV_MCULCD_WRITE_REG(0xD320, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD321, 0x84);
            S_DEV_MCULCD_WRITE_REG(0xD322, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD323, 0xA4);
            S_DEV_MCULCD_WRITE_REG(0xD324, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD325, 0xB7);
            S_DEV_MCULCD_WRITE_REG(0xD326, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD327, 0xCF);
            S_DEV_MCULCD_WRITE_REG(0xD328, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD329, 0xDE);
            S_DEV_MCULCD_WRITE_REG(0xD32A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD32B, 0xF2);
            S_DEV_MCULCD_WRITE_REG(0xD32C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD32D, 0xFE);
            S_DEV_MCULCD_WRITE_REG(0xD32E, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD32F, 0x10);
            S_DEV_MCULCD_WRITE_REG(0xD330, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD331, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD332, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD333, 0x6D);
            S_DEV_MCULCD_WRITE_REG(0xD400, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD401, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD402, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD403, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD404, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD405, 0x3A);
            S_DEV_MCULCD_WRITE_REG(0xD406, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD407, 0x4A);
            S_DEV_MCULCD_WRITE_REG(0xD408, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD409, 0x5C);
            S_DEV_MCULCD_WRITE_REG(0xD40A, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD40B, 0x81);
 
            S_DEV_MCULCD_WRITE_REG(0xD40C, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD40D, 0xA6);
            S_DEV_MCULCD_WRITE_REG(0xD40E, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD40F, 0xE5);
            S_DEV_MCULCD_WRITE_REG(0xD410, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD411, 0x13);
            S_DEV_MCULCD_WRITE_REG(0xD412, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD413, 0x54);
            S_DEV_MCULCD_WRITE_REG(0xD414, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD415, 0x82);
            S_DEV_MCULCD_WRITE_REG(0xD416, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD417, 0xCA);
            S_DEV_MCULCD_WRITE_REG(0xD418, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD419, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD41A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD41B, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD41C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD41D, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD41E, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD41F, 0x67);
            S_DEV_MCULCD_WRITE_REG(0xD420, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD421, 0x84);
            S_DEV_MCULCD_WRITE_REG(0xD422, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD423, 0xA4);
            S_DEV_MCULCD_WRITE_REG(0xD424, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD425, 0xB7);
            S_DEV_MCULCD_WRITE_REG(0xD426, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD427, 0xCF);
            S_DEV_MCULCD_WRITE_REG(0xD428, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD429, 0xDE);
            S_DEV_MCULCD_WRITE_REG(0xD42A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD42B, 0xF2);
            S_DEV_MCULCD_WRITE_REG(0xD42C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD42D, 0xFE);
            S_DEV_MCULCD_WRITE_REG(0xD42E, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD42F, 0x10);
            S_DEV_MCULCD_WRITE_REG(0xD430, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD431, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD432, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD433, 0x6D);
            S_DEV_MCULCD_WRITE_REG(0xD500, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD501, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD502, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD503, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD504, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD505, 0x3A);
            S_DEV_MCULCD_WRITE_REG(0xD506, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD507, 0x4A);
            S_DEV_MCULCD_WRITE_REG(0xD508, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD509, 0x5C);
            S_DEV_MCULCD_WRITE_REG(0xD50A, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD50B, 0x81);
 
            S_DEV_MCULCD_WRITE_REG(0xD50C, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD50D, 0xA6);
            S_DEV_MCULCD_WRITE_REG(0xD50E, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD50F, 0xE5);
            S_DEV_MCULCD_WRITE_REG(0xD510, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD511, 0x13);
            S_DEV_MCULCD_WRITE_REG(0xD512, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD513, 0x54);
            S_DEV_MCULCD_WRITE_REG(0xD514, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD515, 0x82);
            S_DEV_MCULCD_WRITE_REG(0xD516, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD517, 0xCA);
            S_DEV_MCULCD_WRITE_REG(0xD518, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD519, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD51A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD51B, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD51C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD51D, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD51E, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD51F, 0x67);
            S_DEV_MCULCD_WRITE_REG(0xD520, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD521, 0x84);
            S_DEV_MCULCD_WRITE_REG(0xD522, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD523, 0xA4);
            S_DEV_MCULCD_WRITE_REG(0xD524, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD525, 0xB7);
            S_DEV_MCULCD_WRITE_REG(0xD526, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD527, 0xCF);
            S_DEV_MCULCD_WRITE_REG(0xD528, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD529, 0xDE);
            S_DEV_MCULCD_WRITE_REG(0xD52A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD52B, 0xF2);
            S_DEV_MCULCD_WRITE_REG(0xD52C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD52D, 0xFE);
            S_DEV_MCULCD_WRITE_REG(0xD52E, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD52F, 0x10);
            S_DEV_MCULCD_WRITE_REG(0xD530, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD531, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD532, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD533, 0x6D);
            S_DEV_MCULCD_WRITE_REG(0xD600, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD601, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD602, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD603, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD604, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD605, 0x3A);
            S_DEV_MCULCD_WRITE_REG(0xD606, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD607, 0x4A);
            S_DEV_MCULCD_WRITE_REG(0xD608, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD609, 0x5C);
            S_DEV_MCULCD_WRITE_REG(0xD60A, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD60B, 0x81);
 
            S_DEV_MCULCD_WRITE_REG(0xD60C, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD60D, 0xA6);
            S_DEV_MCULCD_WRITE_REG(0xD60E, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD60F, 0xE5);
            S_DEV_MCULCD_WRITE_REG(0xD610, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD611, 0x13);
            S_DEV_MCULCD_WRITE_REG(0xD612, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD613, 0x54);
            S_DEV_MCULCD_WRITE_REG(0xD614, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD615, 0x82);
            S_DEV_MCULCD_WRITE_REG(0xD616, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD617, 0xCA);
            S_DEV_MCULCD_WRITE_REG(0xD618, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD619, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xD61A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD61B, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xD61C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD61D, 0x34);
            S_DEV_MCULCD_WRITE_REG(0xD61E, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD61F, 0x67);
            S_DEV_MCULCD_WRITE_REG(0xD620, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD621, 0x84);
            S_DEV_MCULCD_WRITE_REG(0xD622, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD623, 0xA4);
            S_DEV_MCULCD_WRITE_REG(0xD624, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD625, 0xB7);
            S_DEV_MCULCD_WRITE_REG(0xD626, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD627, 0xCF);
            S_DEV_MCULCD_WRITE_REG(0xD628, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD629, 0xDE);
            S_DEV_MCULCD_WRITE_REG(0xD62A, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD62B, 0xF2);
            S_DEV_MCULCD_WRITE_REG(0xD62C, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xD62D, 0xFE);
            S_DEV_MCULCD_WRITE_REG(0xD62E, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD62F, 0x10);
            S_DEV_MCULCD_WRITE_REG(0xD630, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD631, 0x33);
            S_DEV_MCULCD_WRITE_REG(0xD632, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xD633, 0x6D);
            //LV2 Page 0 enable 
            S_DEV_MCULCD_WRITE_REG(0xF000, 0x55);
            S_DEV_MCULCD_WRITE_REG(0xF001, 0xAA);
            S_DEV_MCULCD_WRITE_REG(0xF002, 0x52);
            S_DEV_MCULCD_WRITE_REG(0xF003, 0x08);
            S_DEV_MCULCD_WRITE_REG(0xF004, 0x00);
            //Display control
            S_DEV_MCULCD_WRITE_REG(0xB100, 0xCC);
            S_DEV_MCULCD_WRITE_REG(0xB101, 0x00);
            //Source hold time
            S_DEV_MCULCD_WRITE_REG(0xB600, 0x05);
            //Gate EQ control
            S_DEV_MCULCD_WRITE_REG(0xB700, 0x70);
            S_DEV_MCULCD_WRITE_REG(0xB701, 0x70);
            //Source EQ control (Mode 2)
            S_DEV_MCULCD_WRITE_REG(0xB800, 0x01);
            S_DEV_MCULCD_WRITE_REG(0xB801, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xB802, 0x03);
            S_DEV_MCULCD_WRITE_REG(0xB803, 0x03);
            //Inversion mode (2-dot)
            S_DEV_MCULCD_WRITE_REG(0xBC00, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xBC01, 0x00);
            S_DEV_MCULCD_WRITE_REG(0xBC02, 0x00);
            //Timing control 4H w/ 4-delay
            S_DEV_MCULCD_WRITE_REG(0xC900, 0xD0);
            S_DEV_MCULCD_WRITE_REG(0xC901, 0x02);
            S_DEV_MCULCD_WRITE_REG(0xC902, 0x50);
            S_DEV_MCULCD_WRITE_REG(0xC903, 0x50);
            S_DEV_MCULCD_WRITE_REG(0xC904, 0x50);
            S_DEV_MCULCD_WRITE_REG(0x3500, 0x00);
            S_DEV_MCULCD_WRITE_REG(0x3A00, 0x55);  //16-bit/pixel
            S_DEV_MCULCD_WR_REG(0x1100);
            soc_time_delay_us(120);
            S_DEV_MCULCD_WR_REG(0x2900);  
            break;

        case DEV_LCD_SSD1963_ID:
            S_DEV_MCULCD_WR_REG(0xE2);        //Set PLL with OSC = 10MHz (hardware),    Multiplier N = 35, 250MHz < VCO < 800MHz = OSC*(N+1), VCO = 300MHz
            S_DEV_MCULCD_WR_DATA(0x1D);        //参数1 
            S_DEV_MCULCD_WR_DATA(0x02);        //参数2 Divider M = 2, PLL = 300/(M+1) = 100MHz
            S_DEV_MCULCD_WR_DATA(0x04);        //参数3 Validate M and N values   
            soc_time_delay_us(100);
            S_DEV_MCULCD_WR_REG(0xE0);        // Start PLL command
            S_DEV_MCULCD_WR_DATA(0x01);        // enable PLL
            soc_time_delay_ms(10);
            S_DEV_MCULCD_WR_REG(0xE0);        // Start PLL command again
            S_DEV_MCULCD_WR_DATA(0x03);        // now, use PLL output as system clock    
            soc_time_delay_ms(12);  
            S_DEV_MCULCD_WR_REG(0x01);        //软复位
            soc_time_delay_ms(10);
    
            S_DEV_MCULCD_WR_REG(0xE6);        //设置像素频率,33Mhz
            S_DEV_MCULCD_WR_DATA(0x2F);
            S_DEV_MCULCD_WR_DATA(0xFF);
            S_DEV_MCULCD_WR_DATA(0xFF);
    
            S_DEV_MCULCD_WR_REG(0xB0);        //设置LCD模式
            S_DEV_MCULCD_WR_DATA(0x20);        //24位模式
            S_DEV_MCULCD_WR_DATA(0x00);        //TFT 模式 

            S_DEV_MCULCD_WR_DATA((DEV_LCD_SSD_HOR_RESOLUTION - 1) >> 8);//设置LCD水平像素
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_HOR_RESOLUTION - 1);         
            S_DEV_MCULCD_WR_DATA((DEV_LCD_SSD_VER_RESOLUTION - 1) >> 8);//设置LCD垂直像素
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_VER_RESOLUTION - 1);         
            S_DEV_MCULCD_WR_DATA(0x00);        //RGB序列 
    
            S_DEV_MCULCD_WR_REG(0xB4);        //Set horizontal period
            S_DEV_MCULCD_WR_DATA((DEV_LCD_SSD_HT - 1) >> 8);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_HT - 1);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_HPS >> 8);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_HPS);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_HOR_PULSE_WIDTH - 1);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_REG(0xB6);        //Set vertical period
            S_DEV_MCULCD_WR_DATA((DEV_LCD_SSD_VT - 1) >> 8);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_VT - 1);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_VPS >> 8);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_VPS);
            S_DEV_MCULCD_WR_DATA(DEV_LCD_SSD_VER_FRONT_PORCH - 1);
            S_DEV_MCULCD_WR_DATA(0x00);
            S_DEV_MCULCD_WR_DATA(0x00);
    
            S_DEV_MCULCD_WR_REG(0xF0);    //设置SSD1963与CPU接口为16bit  
            S_DEV_MCULCD_WR_DATA(0x03);    //16-bit(565 format) data for 16bpp 

            S_DEV_MCULCD_WR_REG(0x29);    //开启显示
            //设置PWM输出  背光通过占空比可调 
            S_DEV_MCULCD_WR_REG(0xD0);    //设置自动白平衡DBC
            S_DEV_MCULCD_WR_DATA(0x00);    //disable
    
            S_DEV_MCULCD_WR_REG(0xBE);    //配置PWM输出
            S_DEV_MCULCD_WR_DATA(0x05);    //1设置PWM频率
            S_DEV_MCULCD_WR_DATA(0xFE);    //2设置PWM占空比
            S_DEV_MCULCD_WR_DATA(0x01);    //3设置C
            S_DEV_MCULCD_WR_DATA(0x00);    //4设置D
            S_DEV_MCULCD_WR_DATA(0x00);    //5设置E 
            S_DEV_MCULCD_WR_DATA(0x00);    //6设置F 
    
            S_DEV_MCULCD_WR_REG(0xB8);    //设置GPIO配置
            S_DEV_MCULCD_WR_DATA(0x03);    //2个IO口设置成输出
            S_DEV_MCULCD_WR_DATA(0x01);    //GPIO使用正常的IO功能 
            S_DEV_MCULCD_WR_REG(0xBA);
            S_DEV_MCULCD_WR_DATA(0X01);    //GPIO[1:0]=01,控制LCD方向
    
            s_dev_mculcd_set_ssd1963_backlight(100);//背光设置为最亮
            break;

        default:
            ret_status = RET_ERROR;
            break;
    }

    return ret_status;
}

/* 如果是这几个IC,则设置WR时序为最快 */
static RET_STATUS s_dev_mculcd_improve_write_speed(uint16_t dev_id)
{
    /* 检验控制IC型号 */
    if ((dev_id != DEV_LCD_ILI9341_ID) && (dev_id != DEV_LCD_NT35310_ID) && \
        (dev_id != DEV_LCD_NT35510_ID) && (dev_id != DEV_LCD_SSD1963_ID))
    {
        return RET_ERROR;
    }

    //重新配置写时序控制寄存器的时序
    FMC_Bank1E->BWTR[0] &= ~(0XF << 0);    //地址建立时间(ADDSET)清零
    FMC_Bank1E->BWTR[0] &= ~(0XF << 8);    //数据保存时间清零
    FMC_Bank1E->BWTR[0] |= (4 << 0);         //地址建立时间(ADDSET)为4个HCLK =21ns
    FMC_Bank1E->BWTR[0] |= (4 << 8);         //数据保存时间(DATAST)为5.2ns*4个HCLK=21ns

    return RET_OK;
}

/* 设置屏幕扫描方向 */
static RET_STATUS s_dev_mculcd_set_scan_dir(uint8_t scan_dir, uint16_t dev_id)
{
    RET_STATUS ret_status = RET_OK;
    uint16_t   dirreg, regval, temp;

    /* 横屏时, 对1963不改变扫描方向, 竖屏时1963改变方向 */
    if (((scan_dir == DEV_LCD_CTRL_HOR) && (dev_id != DEV_LCD_SSD1963_ID)) || \
        ((scan_dir == DEV_LCD_CTRL_VER) && (dev_id == DEV_LCD_SSD1963_ID)))
    {
        switch (scan_dir)
        {
            case DEV_LCD_DIR_L2R_U2D: scan_dir = DEV_LCD_DIR_D2U_L2R; break;
            case DEV_LCD_DIR_L2R_D2U: scan_dir = DEV_LCD_DIR_D2U_R2L; break;
            case DEV_LCD_DIR_R2L_U2D: scan_dir = DEV_LCD_DIR_U2D_L2R; break;
            case DEV_LCD_DIR_R2L_D2U: scan_dir = DEV_LCD_DIR_U2D_R2L; break;
            case DEV_LCD_DIR_U2D_L2R: scan_dir = DEV_LCD_DIR_L2R_D2U; break;
            case DEV_LCD_DIR_U2D_R2L: scan_dir = DEV_LCD_DIR_L2R_U2D; break;
            case DEV_LCD_DIR_D2U_L2R: scan_dir = DEV_LCD_DIR_R2L_D2U; break;
            case DEV_LCD_DIR_D2U_R2L: scan_dir = DEV_LCD_DIR_R2L_U2D; break;
            default: ret_status = RET_ERROR; break;
        }
    }

    /* 9341/5310/5510/1963,特殊处理 */
    if ((dev_id == DEV_LCD_ILI9341_ID) || (dev_id == DEV_LCD_NT35310_ID) || \
        (dev_id == DEV_LCD_NT35510_ID) || (dev_id == DEV_LCD_SSD1963_ID))
    {
        switch (scan_dir)
        {
            case DEV_LCD_DIR_L2R_U2D: regval |= (0 << 7) | (0 << 6) | (0 << 5); break;
            case DEV_LCD_DIR_L2R_D2U: regval |= (1 << 7) | (0 << 6) | (0 << 5); break;
            case DEV_LCD_DIR_R2L_U2D: regval |= (0 << 7) | (1 << 6) | (0 << 5); break;
            case DEV_LCD_DIR_R2L_D2U: regval |= (1 << 7) | (1 << 6) | (0 << 5); break;
            case DEV_LCD_DIR_U2D_L2R: regval |= (0 << 7) | (0 << 6) | (1 << 5); break;
            case DEV_LCD_DIR_U2D_R2L: regval |= (0 << 7) | (1 << 6) | (1 << 5); break;
            case DEV_LCD_DIR_D2U_L2R: regval |= (1 << 7) | (0 << 6) | (1 << 5); break;
            case DEV_LCD_DIR_D2U_R2L: regval |= (1 << 7) | (1 << 6) | (1 << 5); break;
            default: ret_status = RET_ERROR; break;
        }

        if (dev_id == DEV_LCD_NT35510_ID)
        {
            dirreg = 0x3600;
        }
        else
        {
            dirreg = 0x36;
        }

        //5310/5510/1963不需要BGR
        if ((dev_id != DEV_LCD_NT35310_ID) && (dev_id != DEV_LCD_NT35510_ID) && \
            (dev_id != DEV_LCD_SSD1963_ID))
        {
            regval |= 0x08;
        }

        S_DEV_MCULCD_WRITE_REG(dirreg, regval);

        /* 1963不做坐标处理 */
        if (dev_id != DEV_LCD_SSD1963_ID)
        {
            if (regval & 0x20)
            {
                if (s_dev_mculcd.width < s_dev_mculcd.height)    //交换X,Y
                {
                    temp                  = s_dev_mculcd.width;
                    s_dev_mculcd.width  = s_dev_mculcd.height;
                    s_dev_mculcd.height = temp;
                }
            }
            else
            {
                if (s_dev_mculcd.width > s_dev_mculcd.height)  //交换X,Y
                {
                    temp                  = s_dev_mculcd.width;
                    s_dev_mculcd.width  = s_dev_mculcd.height;
                    s_dev_mculcd.height = temp;
                }
            }
        }

        if (dev_id == DEV_LCD_NT35510_ID)
        {
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd);
            S_DEV_MCULCD_WR_DATA(0); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd + 1);
            S_DEV_MCULCD_WR_DATA(0); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd + 2);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.width - 1) >> 8); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd + 3);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.width - 1) & 0XFF); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd);
            S_DEV_MCULCD_WR_DATA(0); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd + 1);
            S_DEV_MCULCD_WR_DATA(0); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd + 2);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.height - 1) >> 8); 
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd + 3);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.height - 1) & 0XFF);
        }
        else
        {
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd); 
            S_DEV_MCULCD_WR_DATA(0);
            S_DEV_MCULCD_WR_DATA(0);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.width - 1) >> 8);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.width - 1) & 0XFF);
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd); 
            S_DEV_MCULCD_WR_DATA(0);
            S_DEV_MCULCD_WR_DATA(0);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.height - 1) >> 8);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.height - 1) & 0XFF);  
        }
    }
    else
    {
        ret_status = RET_ERROR;
    }

    return ret_status;
}

/* 设置屏幕显示方向和尺寸 */
static RET_STATUS s_dev_mculcd_set_display_dir(uint8_t ctrl_dir, uint16_t dev_id)
{
    uint16_t temp;

    /* 默认竖屏方向 */
    if (dev_id == DEV_LCD_ILI9341_ID)
    {
        s_dev_mculcd.wr_gram_cmd  = 0x2C;    /* 设置写入GRAM的指令  */
        s_dev_mculcd.set_xpos_cmd = 0x2A;    /* 设置写X坐标指令 */
        s_dev_mculcd.set_ypos_cmd = 0x2B;    /* 设置写Y坐标指令 */
        s_dev_mculcd.width        = 240;     /* 设置宽度像素 */
        s_dev_mculcd.height       = 320;     /* 设置高度像素 */
    }
    else if (dev_id == DEV_LCD_NT35310_ID)
    {
        s_dev_mculcd.wr_gram_cmd  = 0x2C;    /* 设置写入GRAM的指令  */
        s_dev_mculcd.set_xpos_cmd = 0x2A;    /* 设置写X坐标指令 */
        s_dev_mculcd.set_ypos_cmd = 0x2B;    /* 设置写Y坐标指令 */
        s_dev_mculcd.width        = 320;
        s_dev_mculcd.height       = 480;
    }
    else if (dev_id == DEV_LCD_NT35510_ID)
    {
        s_dev_mculcd.wr_gram_cmd  = 0X2C00;
        s_dev_mculcd.set_xpos_cmd = 0X2A00;
        s_dev_mculcd.set_ypos_cmd = 0X2B00;
        s_dev_mculcd.width        = 480;
        s_dev_mculcd.height       = 800;
    }
    else if (dev_id == DEV_LCD_SSD1963_ID)
    {
        s_dev_mculcd.wr_gram_cmd  = 0X2C;
        s_dev_mculcd.set_xpos_cmd = 0X2B;
        s_dev_mculcd.set_ypos_cmd = 0X2A;
        s_dev_mculcd.width        = 480;
        s_dev_mculcd.height       = 800;
    }
    else
    {
        return RET_ERROR;    /* 未找到匹配的IC型号 */
    }

    /* 横屏交换像素信息 */
    if (ctrl_dir == DEV_LCD_CTRL_HOR)
    {
        temp                = s_dev_mculcd.width;
        s_dev_mculcd.width  = s_dev_mculcd.height;
        s_dev_mculcd.height = temp;
    }

    /* 记录控制方向*/
    s_dev_mculcd.ctrl_dir = ctrl_dir;

    /* 设置扫描方向 */
    s_dev_mculcd_set_scan_dir(DEV_LCD_DIR_DEFAULT, dev_id);

    return RET_OK;
}

/* lcd 显示控制 */
void s_dev_mculcd_display_switch(uint8_t status)
{
    if (status == DEV_LCD_DISPLAY_ON)   /* 开启显示 */
    {
        if ((s_dev_mculcd.id == DEV_LCD_ILI9341_ID) || (s_dev_mculcd.id == DEV_LCD_NT35310_ID) || \
            (s_dev_mculcd.id == DEV_LCD_SSD1963_ID))
        {
            S_DEV_MCULCD_WR_REG(0x29);
        }
        else if (s_dev_mculcd.id == DEV_LCD_NT35510_ID)
        {
            S_DEV_MCULCD_WR_REG(0x2900);
        }
    }
    else if (status == DEV_LCD_DISPLAY_OFF) /* 关闭显示 */
    {
        if ((s_dev_mculcd.id == DEV_LCD_ILI9341_ID) || (s_dev_mculcd.id == DEV_LCD_NT35310_ID) || \
            (s_dev_mculcd.id == DEV_LCD_SSD1963_ID))
        {
            S_DEV_MCULCD_WR_REG(0x28);
        }
        else if (s_dev_mculcd.id == DEV_LCD_NT35510_ID)
        {
            S_DEV_MCULCD_WR_REG(0x2800);
        }
    }

    return;
}

/* 背光灯控制 */
static void s_dev_mculcd_backlight_ctrl(uint8_t status)
{
    if (status == DEV_LCD_BACKLIGHT_ON)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
    }
    else if (status == DEV_LCD_BACKLIGHT_OFF)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
    }

    return;
}

/* 设置画笔起始位置(对RGB屏无效) */
static void s_dev_mculcd_set_cursor(uint16_t xpos, uint16_t ypos)
{
    /* 检验数据合法性 */
    if ((xpos > s_dev_mculcd.width) || (ypos > s_dev_mculcd.height))
    {
        return;
    }

    if ((s_dev_mculcd.id == DEV_LCD_ILI9341_ID) || (s_dev_mculcd.id == DEV_LCD_NT35310_ID))
    {
        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd);
        S_DEV_MCULCD_WR_DATA(xpos >> 8);
        S_DEV_MCULCD_WR_DATA(xpos & 0XFF);
        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd);
        S_DEV_MCULCD_WR_DATA(ypos >> 8);
        S_DEV_MCULCD_WR_DATA(ypos & 0XFF);
    }
    else if (s_dev_mculcd.id == DEV_LCD_SSD1963_ID)
    {
        if (s_dev_mculcd.ctrl_dir == DEV_LCD_CTRL_VER)//x坐标需要变换
        {
            xpos = s_dev_mculcd.width - 1 - xpos;
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd);
            S_DEV_MCULCD_WR_DATA(0);
            S_DEV_MCULCD_WR_DATA(0);
            S_DEV_MCULCD_WR_DATA(xpos >> 8);
            S_DEV_MCULCD_WR_DATA(xpos & 0XFF);
        }
        else
        {
            S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd);
            S_DEV_MCULCD_WR_DATA(xpos >> 8);
            S_DEV_MCULCD_WR_DATA(xpos & 0XFF);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.width - 1) >> 8);
            S_DEV_MCULCD_WR_DATA((s_dev_mculcd.width - 1) & 0XFF);
        }

        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd);
        S_DEV_MCULCD_WR_DATA(ypos >> 8);
        S_DEV_MCULCD_WR_DATA(ypos & 0XFF);
        S_DEV_MCULCD_WR_DATA((s_dev_mculcd.height - 1) >> 8);
        S_DEV_MCULCD_WR_DATA((s_dev_mculcd.height - 1) & 0XFF);
    }
    else if(s_dev_mculcd.id == DEV_LCD_NT35510_ID)
    {
        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd);
        S_DEV_MCULCD_WR_DATA(xpos >> 8);
        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_xpos_cmd + 1);
        S_DEV_MCULCD_WR_DATA(xpos & 0XFF);
        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd);
        S_DEV_MCULCD_WR_DATA(ypos >> 8);
        S_DEV_MCULCD_WR_REG(s_dev_mculcd.set_ypos_cmd + 1);
        S_DEV_MCULCD_WR_DATA(ypos & 0XFF);
    }
    else
    {
        return;
    }

    return;
}

/* 屏幕填充 指定区域和指定颜色 函数 */
static void s_dev_mculcd_fill_color(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint32_t color)
{
    uint16_t i, j, xlen = 0;

    xlen = ex - sx + 1;

    for (i = sy; i <= ey; i++)
    {
        /* 设置光标位置  */
        s_dev_mculcd_set_cursor(sx, i);

        /* 开始写入GRAM */
        s_dev_mculcd_write_gram_cmd();
    
        /* 写入颜色 */
        for (j = 0; j < xlen; j++)
        {
            LCD->Ram = (uint16_t) color;
        }
    }

    return;
}

/* 屏幕填充 指定区域和颜色集 函数 */
static void s_dev_mculcd_fill_colors(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t *colors)
{
    uint16_t i, j, height, width;

    width  = ex - sx + 1;
    height = ey - sy + 1;

    for (i = 0; i <= height; i++)
    {
        /* 设置光标位置  */
        s_dev_mculcd_set_cursor(sx, sy + i);

        /* 开始写入GRAM */
        s_dev_mculcd_write_gram_cmd();
    
        /* 写入颜色 */
        for (j = 0; j < width; j++)
        {
            LCD->Ram = colors[i * width + j];
        }
    }

    return;
}

/* 清屏 */
static void s_dev_mculcd_clear(void)
{
    /* 填入白色 */
    s_dev_mculcd_fill_color(0, 0, (s_dev_mculcd.width - 1), (s_dev_mculcd.height - 1), LCD_COLOR_WHITE);

    return;
}

/* 画点 */
void s_dev_mculcd_draw_point(uint16_t xpos, uint16_t ypos, uint32_t color)
{
    /* 设置画点起始位置 */
    s_dev_mculcd_set_cursor(xpos, ypos);

    /* 开始写入GRAM */
    s_dev_mculcd_write_gram_cmd();

    /* 写入颜色 */
    LCD->Ram = color;

    return;
}

/* MCULCD 初始化 */
static RET_STATUS s_dev_mculcd_init(void)
{
    uint16_t dev_id = 0;

    GPIO_InitTypeDef          GPIO_Initure;
    FMC_NORSRAM_TimingTypeDef FMC_ReadTiming;
    FMC_NORSRAM_TimingTypeDef FMC_WriteTiming;

    __HAL_RCC_GPIOB_CLK_ENABLE();               //开启GPIOB时钟

    GPIO_Initure.Pin   = GPIO_PIN_5;            //PB5,背光控制
    GPIO_Initure.Mode  = GPIO_MODE_OUTPUT_PP;   //推挽输出
    GPIO_Initure.Pull  = GPIO_PULLUP;           //上拉
    GPIO_Initure.Speed = GPIO_SPEED_HIGH;       //高速
    HAL_GPIO_Init(GPIOB, &GPIO_Initure); 
    
    SRAM_Handler.Instance                = FMC_NORSRAM_DEVICE;
    SRAM_Handler.Extended                = FMC_NORSRAM_EXTENDED_DEVICE;
    SRAM_Handler.Init.NSBank             = FMC_NORSRAM_BANK1;     //使用NE1
    SRAM_Handler.Init.DataAddressMux     = FMC_DATA_ADDRESS_MUX_DISABLE; //地址/数据线不复用
    SRAM_Handler.Init.MemoryType         = FMC_MEMORY_TYPE_SRAM;   //SRAM
    SRAM_Handler.Init.MemoryDataWidth    = FMC_NORSRAM_MEM_BUS_WIDTH_16; //16位数据宽度
    SRAM_Handler.Init.BurstAccessMode    = FMC_BURST_ACCESS_MODE_DISABLE; //是否使能突发访问,仅对同步突发存储器有效,此处未用到
    SRAM_Handler.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;//等待信号的极性,仅在突发模式访问下有用
    SRAM_Handler.Init.WaitSignalActive   = FMC_WAIT_TIMING_BEFORE_WS;   //存储器是在等待周期之前的一个时钟周期还是等待周期期间使能NWAIT
    SRAM_Handler.Init.WriteOperation     = FMC_WRITE_OPERATION_ENABLE;    //存储器写使能
    SRAM_Handler.Init.WaitSignal         = FMC_WAIT_SIGNAL_DISABLE;           //等待使能位,此处未用到
    SRAM_Handler.Init.ExtendedMode       = FMC_EXTENDED_MODE_ENABLE;        //读写使用不同的时序
    SRAM_Handler.Init.AsynchronousWait   = FMC_ASYNCHRONOUS_WAIT_DISABLE;//是否使能同步传输模式下的等待信号,此处未用到
    SRAM_Handler.Init.WriteBurst         = FMC_WRITE_BURST_DISABLE;           //禁止突发写
    SRAM_Handler.Init.ContinuousClock    = FMC_CONTINUOUS_CLOCK_SYNC_ASYNC;

    //FMC读时序控制寄存器
    FMC_ReadTiming.AddressSetupTime = 0x0F;        //地址建立时间(ADDSET)为15个HCLK 1/180M*15=5.5ns*15=82.5ns
    FMC_ReadTiming.AddressHoldTime  = 0x00;
    FMC_ReadTiming.DataSetupTime    = 0x46;           //数据保存时间(DATAST)为70个HCLK    =5.5*70=385ns
    FMC_ReadTiming.AccessMode       = FMC_ACCESS_MODE_A; //模式A

    //FMC写时序控制寄存器
    FMC_WriteTiming.AddressSetupTime = 0x0F;            //地址建立时间(ADDSET)为15个HCLK=82.5ns
    FMC_WriteTiming.AddressHoldTime  = 0x00;
    FMC_WriteTiming.DataSetupTime    = 0x0F;               //数据保存时间(DATAST)为5.5ns*15个HCLK=82.5ns
    FMC_WriteTiming.AccessMode       = FMC_ACCESS_MODE_A;     //模式A

    if (HAL_SRAM_Init(&SRAM_Handler, &FMC_ReadTiming, &FMC_WriteTiming) != HAL_OK)
    {
        return RET_ERROR;
    }

    soc_time_delay_ms(50); // delay 50 ms 

    /* 获得LCD设备ID */
    dev_id = s_dev_mculcd_get_id();

    /* LCD寄存器配置 */
    if (s_dev_mculcd_reg_init(dev_id) != RET_OK)
    {
        return RET_ERROR; /* 初始化失败 */
    }

    /* 初始化完成以后, 则设置WR时序为最快 */
    if (s_dev_mculcd_improve_write_speed(dev_id) != RET_OK)
    {
        return RET_ERROR; /* 初始化失败 */
    }

    /* 默认为竖屏 */
    {
    if (s_dev_mculcd_set_display_dir(DEV_LCD_CTRL_DIR_DEFAULT, dev_id) != RET_OK)
        return RET_ERROR; /* 初始化失败 */
    }

    /* 打开背光 */
    s_dev_mculcd_backlight_ctrl(DEV_LCD_BACKLIGHT_ON);

    /* 清屏(屏幕显示初始化) */
    s_dev_mculcd_clear();

    return RET_OK;
}

/* 显示屏幕信息 */
void s_dev_mculcd_show_info(void)
{
    printf("MCULCD info: \r\n");
    printf("ID: 0x%x\r\n", s_dev_mculcd.id);    /* id */
    printf("Width = %d, Height: %d\r\n", s_dev_mculcd.width, s_dev_mculcd.height); /* 显示器宽和高 */
    printf("Ctrl Dir = %s\r\n", (s_dev_mculcd.ctrl_dir == DEV_LCD_CTRL_VER) ? "vertical" : "horizontal"); /* 显示方向 */

    return;
}

//SRAM底层驱动，时钟使能，引脚分配
//此函数会被HAL_SRAM_Init()调用
//hsram:SRAM句柄
void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
{
    GPIO_InitTypeDef GPIO_Initure;

    __HAL_RCC_FMC_CLK_ENABLE();                //使能FMC时钟
    __HAL_RCC_GPIOD_CLK_ENABLE();            //使能GPIOD时钟
    __HAL_RCC_GPIOE_CLK_ENABLE();            //使能GPIOE时钟
    
    //初始化PD0,1,4,5,7,8,9,10,13,14,15
    GPIO_Initure.Pin       = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8| \
                             GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
    GPIO_Initure.Mode      = GPIO_MODE_AF_PP;         //推挽复用
    GPIO_Initure.Pull      = GPIO_PULLUP;            //上拉
    GPIO_Initure.Speed     = GPIO_SPEED_HIGH;        //高速
    GPIO_Initure.Alternate = GPIO_AF12_FMC;    //复用为FMC
    HAL_GPIO_Init(GPIOD, &GPIO_Initure);     //初始化
    
    //初始化PE7,8,9,10,11,12,13,14,15
    GPIO_Initure.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11| \
                       GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
    HAL_GPIO_Init(GPIOE, &GPIO_Initure);

    return;
}

/********************************************************************************************************
 *                                               LCD 对外接口
 *******************************************************************************************************/

/* LCD初始化 (兼容 MCULCD 和 RGBLCD) */
RET_STATUS dev_lcd_init(void)
{
    /* 优先初始化 RGB屏 */
    if (s_dev_rgblcd_init() == RET_OK)
    {
        /* 标记屏幕类型 */
        s_dev_lcd.type = DEV_LCD_TYPE_RGB;

        return RET_OK;
    }

    /* 初始化 RGB屏失败就初始化 MCU屏 */
    if (s_dev_mculcd_init() == RET_OK)
    {
        /* 标记屏幕类型 */
        s_dev_lcd.type = DEV_LCD_TYPE_MCU;

        return RET_OK;
    }

    /* 返回失败 */
    return RET_ERROR;
}

/* 显示lcd信息 */
void dev_lcd_show_info(void)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_show_info();
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_show_info();
    }

    return;
}

/* 背光灯控制 */
void dev_lcd_backlight_ctrl(uint8_t status)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_backlight_ctrl(status);
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_backlight_ctrl(status);
    }

    return;
}

/* 显示开关 */
void dev_lcd_display_switch(uint8_t status)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_display_switch(status);
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_display_switch(status);
    }

    return;
}

/* 设置光标 */
void dev_lcd_set_cursor(uint16_t xpos, uint16_t ypos)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        return; /* 不需要光标 */
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_set_cursor(xpos, ypos);
    }

    return;
}

/* 屏幕填充 指定区域和指定颜色 函数 */
void dev_lcd_fill_color(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint32_t color)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_fill_color(sx, sy, ex, ey, color);
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_fill_color(sx, sy, ex, ey, color);
    }

    return;
}

/* 屏幕填充 指定区域和颜色集 函数 */
void dev_lcd_fill_colors(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t *colors)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_fill_colors(sx, sy, ex, ey, colors);
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_fill_colors(sx, sy, ex, ey, colors);
    }

    return;
}

/* 清屏 */
void dev_lcd_clear(void)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_clear();
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_clear();
    }

    return;
}

/* 画点 */
void dev_lcd_draw_point(uint16_t xpos, uint16_t ypos, uint32_t color)
{
    /* 判断LCD类型 */
    if (s_dev_lcd.type == DEV_LCD_TYPE_RGB)
    {
        s_dev_rgblcd_draw_point(xpos, ypos, color);
    }
    else if (s_dev_lcd.type == DEV_LCD_TYPE_MCU)
    {
        s_dev_mculcd_draw_point(xpos, ypos, color);
    }

    return;
}
